# Replication file for the main analysis in Nasr (2022)
# Date: 7-Jan-2022
# Data used: exp_data_all_wide.csv

# Main variables:
#             `self_placement_exp`: respondent self-placement on the experimental question
#             `exp2_condiiton`: experimental condition
#             `perceived_competence_A`: competence rating of party A
#             `perceived_competence_B`: competence rating of party B
#             `vote_A`: binary variable (1 if respondent selected party A, 0 otherwise)
#             `vote_B`: binary variable (1 if respondent selected party B, 0 otherwise)
#             `clear_party_position`: the position of party A (randomized; 1= euroskeptic; 2=europhile)


rm(list=ls())
dev.off()
# Set working directory first
data <- read.csv("exp_data_all_wide.csv")

library(tidyr) 
library(estimatr)
library(dplyr)
library(ggplot2)
library(broom)
library(gridExtra)
library(stargazer)

# Create dependent variables: 
data$vote_diff = data$vote_B - data$vote_A # vote differential
data$comp_diff = data$perceived_competence_B - data$perceived_competence_A # competence rating differential



# -------------------------------------------
# First, the average effect of ambiguity

# (1) Vote choice
summary_df <-
  data %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         Party = "Precise") # obtain vote_diff estimates within experimental groups with SEs and CIs

summary_df$group<- case_when(summary_df$group == "vague" ~ "Vague", 
                             summary_df$group == "ambivalent"~ "Ambivalent", 
                             summary_df$group == "negative" ~ "Negative", 
                             summary_df$group == "switcher" ~ "Switching") # Capitalize
summary_df$group <- factor(summary_df$group, levels = c("Vague", "Ambivalent", "Switching" ,"Negative"))



a= summary_df %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(y= "Net advantage of ambiguous party in %", x="") + 
  geom_text(mapping = aes(x=1.15, y=0.13, label = "0.125"), size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.065, label = "0.06"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.08, label = "-0.088"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.088, label = "-0.097"), size = 5) + 
  theme(text = element_text(size=20),
        axis.text.x = element_text( hjust=1)) ; a




ggsave("main.pdf", plot = a, device = "pdf", width = 10, height = 6) # Save figure

# T test
t.test(data$vote_diff[data$exp2_condition=="vague"], data$vote_diff[data$exp2_condition=="ambivalent"])
t.test(data$vote_diff[data$exp2_condition=="negative"], data$vote_diff[data$exp2_condition=="switcher"])
t.test(data$vote_diff[data$exp2_condition=="ambivalent"], data$vote_diff[data$exp2_condition=="switcher"])

# (2) Competence rating

summary_df <-
  data %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(comp_diff ~ 1, data = .))) %>%
  mutate(clarity = estimate, 
         group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         Party = "Precise")


summary_df$group<- case_when(summary_df$group == "vague" ~ "Vague", 
                             summary_df$group == "ambivalent"~ "Ambivalent", 
                             summary_df$group == "negative" ~ "Negative", 
                             summary_df$group == "switcher" ~ "Switching")
summary_df$group <- factor(summary_df$group, levels = c("Vague", "Ambivalent","Switching" ,"Negative"))


b= summary_df %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(y= "Net advantage of ambiguous party", x="") + 
  geom_text(mapping = aes(x=1.15, y=0.5, label = "0.48"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.13, label = "0.12"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.7, label = "-0.57"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.5, label = "-0.86"), size = 5) + 
  theme(text = element_text(size=20),
        axis.text.x = element_text( hjust=1)); b


t.test(data$comp_diff[data$exp2_condition=="vague"], data$comp_diff[data$exp2_condition=="ambivalent"])
t.test(data$comp_diff[data$exp2_condition=="negative"], data$comp_diff[data$exp2_condition=="switcher"])


ggsave("comp.pdf", plot = b, device = "pdf", width = 10, height = 6) # save figure


# ----------------------------------------------
# Second, we create the datasets for the three scenarios 


# Clean DKs first
table(data$self_placement_exp) # check before
data$self_placement_exp[data$self_placement_exp>10] = 5 # recode DKs to the midpoint
table(data$self_placement_exp) # check after


# Scenario 1: the rival party agrees with the voter

sc1 = data %>% dplyr::filter(clear_party_position==2 & self_placement_exp>5 | 
                               clear_party_position==1 & self_placement_exp<5)

# Scenario 2: the rival party disagrees with the voter

sc2 = data %>% dplyr::filter(clear_party_position==2 & self_placement_exp<5 | 
                               clear_party_position==1 & self_placement_exp>5)

# Scenario 3: when the voter is indifferent or undecided

sc3 = data %>% dplyr::filter(self_placement_exp==5)

# Estimates with confidence intervals
one <-
  sc1 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         type = "Disadvantegeous")
two <-
  sc2 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         type = "Advanteguous")
three <-
  sc3 %>%
  group_by(exp2_condition) %>%
  do(tidy(lm_robust(vote_diff ~ 1, data = .))) %>%
  mutate(group = factor(exp2_condition, levels = c("vague", "ambivalent", "negative", "switcher")), 
         type = "Indifferent")

# Labels
one$group<- case_when(one$group == "vague" ~ "Vague", 
                             one$group == "ambivalent"~ "Ambivalent", 
                             one$group == "negative" ~ "Negative", 
                             one$group == "switcher" ~ "Switching")
one$group <- factor(one$group, levels = c("Vague", "Ambivalent", "Negative", "Switching"))

two$group<- case_when(two$group == "vague" ~ "Vague", 
                      two$group == "ambivalent"~ "Ambivalent", 
                      two$group == "negative" ~ "Negative", 
                      two$group == "switcher" ~ "Switching")
two$group <- factor(two$group, levels = c("Vague", "Ambivalent", "Negative", "Switching"))

three$group<- case_when(three$group == "vague" ~ "Vague", 
                      three$group == "ambivalent"~ "Ambivalent", 
                      three$group == "negative" ~ "Negative", 
                      three$group == "switcher" ~ "Switching")
three$group <- factor(three$group, levels = c("Vague", "Ambivalent", "Negative", "Switching"))




c1= one %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 1: Disadvantageous competition",
       y="", 
       color = "", tag = "A") +
  geom_text(mapping = aes(x=1.15, y=-0.20, label = "-0.23"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=-0.22, label = "-0.25"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.39, label = "-0.42"), size = 5) + 
  geom_text(mapping = aes(x=4.22, y=-0.39, label = "-0.39"), size = 5) +
  theme(axis.text=element_text(size=16, face = "bold"))+
  labs(x=""); c1


c2= two %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 2: Advantageous competition",
       y="", 
       color = "", tag = "B") +
  theme(axis.text=element_text(size=16, face = "bold"))+
  geom_text(mapping = aes(x=1.15, y=0.46, label = "0.44"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.38, label = "0.36"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=0.27, label = "0.22"), size = 5) + 
  geom_text(mapping = aes(x=4.17, y=0.176, label = "0.18"), size = 5) +
  labs(x=""); c2

c3= three %>% ggplot(aes(x=group, y=estimate)) + 
  geom_point() + 
  geom_pointrange(aes(ymin=conf.low, ymax=conf.high)) + 
  geom_line(group = 1) + 
  ggthemes::theme_base() +  
  geom_hline(yintercept = 0, lty=2)+
  labs(x= "Competence differential (> ambiguous)", title = "Scenario 3: Neutral competition",
       y="", 
       color = "", tag = "C") +
  theme(axis.text=element_text(size=16, face = "bold"))+
  geom_text(mapping = aes(x=1.15, y=0.2, label = "0.17"),  size = 5) +
  geom_text(mapping = aes(x=2.15, y=0.1, label = "0.07"), size = 5) + 
  geom_text(mapping = aes(x=3.15, y=-0.03, label = "-0.07"), size = 5) + 
  geom_text(mapping = aes(x=4.15, y=-0.02, label = "-0.05"), size = 5) +
  labs(x=""); c3

# Combine into one figure
layout_matrix <- matrix(c(1, 1, 2, 2, 4, 3, 3, 4), nrow = 2, byrow = TRUE)
plot= grid.arrange(c1, c2, c3, layout_matrix = layout_matrix); plot


ggsave("sc.pdf", plot = plot, device = "pdf", width = 15, height = 8.71) # Save figure


# -------------------------------------

# Balance test
  
# I first create a numeric variable indicating the experimental condition
  
table(data$exp2_condition)
data <- data %>% mutate(condition = case_when(exp2_condition == "vague" ~ 1, 
                                              exp2_condition == "ambivalent" ~ 2, 
                                              exp2_condition == "negative" ~ 3, 
                                              exp2_condition == "switcher" ~ 4)); table(data$condition)

# Run simple ols regression:
m= lm(condition ~ age_grp_all + female + religion + 
        LR_selfplacement + altruism + trust + 
        risk + democ_satis_country + pessimism_eu + 
        have_voice_in_eu, data); summary(m) # balance test for the full sample

sc1 = data %>% dplyr::filter(clear_party_position==2 & self_placement_exp>5 | 
                               clear_party_position==1 & self_placement_exp<5)

sc2 = data %>% dplyr::filter(clear_party_position==2 & self_placement_exp<5 | 
                               clear_party_position==1 & self_placement_exp>5)

sc3 = data %>% dplyr::filter(self_placement_exp==5 | is.na(self_placement_exp))

m1= lm(condition ~ age_grp_all + female + religion + 
         LR_selfplacement + altruism + trust + 
         risk + democ_satis_country + pessimism_eu + 
         have_voice_in_eu, sc1); summary(m1) # Scenario 1

m2= lm(condition ~ age_grp_all + female + religion + 
         LR_selfplacement + altruism + trust + 
         risk + democ_satis_country + pessimism_eu + 
         have_voice_in_eu, sc2); summary(m2) # Scenario 2

m3 = lm(condition ~ age_grp_all + female + religion + 
          LR_selfplacement + altruism + trust + 
          risk + democ_satis_country + pessimism_eu + 
          have_voice_in_eu, sc3); summary(m3) # scenario 3


stargazer::stargazer(m, m1, m2, m3)


# ------------------------------------

# Alternatice DV
# Pr voting for the ambiguous party as a dv  

m1 <- lm(vote_B~ exp2_condition, data); summary(m1) # no controls
m2 <- lm(vote_B~ exp2_condition + self_placement_exp, data); summary(m2) # control for self-placement
m3 <- lm(vote_B~ exp2_condition + self_placement_exp + factor(country), data); summary(m3) # with country FE

stargazer::stargazer(m1, m2, m3)

# With robust SEs

m1 <- lm_robust(vote_B~ exp2_condition, data, se_type = "HC0"); summary(m1) # no controls
m2 <- lm_robust(vote_B~ exp2_condition + self_placement_exp, data, se_type = "HC0"); summary(m2) # control for self-placement
m3 <- lm_robust(vote_B~ exp2_condition + self_placement_exp + factor(country), data, se_type = "HC0"); summary(m3) # with country FE

# With logistic regression

m1 <- glm(vote_B~ exp2_condition, data, family = "binomial"); summary(m1) # no controls
m2 <- glm(vote_B~ exp2_condition + self_placement_exp, data, family = "binomial"); summary(m2) # control for self-placement
m3 <- glm(vote_B~ exp2_condition + self_placement_exp + factor(country), data, family = "binomial"); summary(m3) # with country FE
